package com.zsule.controllers.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zsule.constants.CoreConstant;
import com.zsule.controllers.BaseController;
import com.zsule.entity.EOrder;
import com.zsule.service.IOrderService;
import com.zsule.service.IUserService;

@Controller
//@RequestMapping("/euser")
@RequestMapping("/order")
public class OrderController extends BaseController {
	private static final Logger logger = Logger.getLogger(OrderController.class);
	@Autowired
	private IUserService euserService;
	@Autowired
	private IOrderService eorderService;

	/*
	 * 商家接单
	 */
	@ResponseBody
	//@RequestMapping(value = "/receiveOrder", method = RequestMethod.POST)
	@RequestMapping(value = "/receive", method = RequestMethod.POST)
	public boolean receiveOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),
					CoreConstant.EORDER_STATUS_RECEIVED);
		} catch (Exception e) {
			logger.error("OrderController.receiveOrder", e);
			return false;
		}
		return true;
	}

	/*
	 * 商家拒绝接单
	 */
	@ResponseBody
	@RequestMapping(value = "/refuseOrder", method = RequestMethod.POST)
	public boolean refuseOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),CoreConstant.EORDER_STATUS_REFUSE);
		} catch (Exception e) {
			logger.error("OrderController.refuseOrder", e);
			return false;
		}
		return true;
	}

	/*
	 * 取消订单
	 */
	@ResponseBody
	//@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	public boolean cancelOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),CoreConstant.EORDER_STATUS_CANCEL);
		} catch (Exception e) {
			logger.error("OrderController.refuseOrder", e);
			return false;
		}
		return true;
	}

	/*
	 * 更改订单状态，订单已经配送
	 */
	@ResponseBody
	//@RequestMapping(value = "/doOrder", method = RequestMethod.POST)
	@RequestMapping(value = "/send", method = RequestMethod.POST)
	public boolean doOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),CoreConstant.EORDER_STATUS_DOING);
		} catch (Exception e) {
			logger.error("OrderController.refuseOrder", e);
			return false;
		}
		return true;
	}

	/*
	 * 更改订单状态，订单已经完成
	 */
	@ResponseBody
//	@RequestMapping(value = "/finishOrder", method = RequestMethod.POST)
	@RequestMapping(value = "/finish", method = RequestMethod.POST)
	public boolean finishOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),CoreConstant.EORDER_STATUS_FINISHED);
		} catch (Exception e) {
			logger.error("OrderController.refuseOrder", e);
			return false;
		}
		return true;
	}
	
	@ResponseBody
	@RequestMapping(value = "/picking", method = RequestMethod.POST)
	public boolean pickingOrder(@RequestBody EOrder eorder,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			eorderService.changeOrderStauts(eorder.getId(),CoreConstant.EORDER_STATUS_PICKING);
		} catch (Exception e) {
			logger.error("OrderController.refuseOrder", e);
			return false;
		}
		return true;
	}
}
